<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-29 14:17:19
 * @LastEditors: your name
 * @LastEditTime: 2021-04-30 12:36:54
 * @Description: file content
-->
<template>
  <el-dialog
    :visible="showVisible"
    center
    :show-close="false"
    width="600px"
  >
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ title }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>

    <el-form
      class="pl30 pr30"
      :model="formData"
      ref="form"
      label-width="120px"
      label-position="right"
    >
      <el-form-item
        :label="title"
        prop="ids"
        :rules="[
          { required: true, message: `请选择${title}`, trigger: 'change' },
        ]"
      >
        <el-select
          v-model="formData.ids"
          :multiple="multiple"
          filterable
          style="width: 400px"
        >
          <el-option v-for="item in list" :key="item[option.id]" :value="item[option.id]" :label="item[option.name]"/>
        </el-select>
      </el-form-item>

    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    multiple: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    list: {
      type: Array,
      default: () => []
    },
    option: { // 字段映射
      type: Object,
      default: () => ({ id: 'id', name: 'name' })
    }
  },
  data () {
    return {
      loading: false,
      showVisible: false,
      formData: {},
      callback: null
    }
  },

  methods: {
    open (data = {}, callback = null) {
      this.$set(this.formData, 'ids', undefined)
      this.showVisible = true
      this.callback = callback
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },

    async submit () {
      this.loading = true
      try {
        await this.$refs.form.validate()
        const { ids } = this.formData
        this.callback && this.callback(ids)
        this.showVisible = false
      } catch (error) {
        console.log(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
